

// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentDate: '',
    list: [ {  title: '标题a', content:"内容a"},
            {  title: '标题b', content:"内容b"},
            {  title: '标题c', content:"内容c"},
            {  title: '标题d', content:"内容d"}],
    title:[],
    titleadd:'',
    contentadd:'',
    titledel:'',
    contentdel:'',
    titlere:'',
    contentre:'',
    instance:'',
    content:[],
    result:[] ,
    date: '',
    show: false,
    showadd: false,
    showre:false,
    showcontent:false,
    active: 0,
    show: false,
    icon: {
      normal: 'https://img.yzcdn.cn/vant/user-inactive.png',
      active: 'https://img.yzcdn.cn/vant/user-active.png',
    },

    value2: 'a',
  },

  onTitleInputre: function(e) {
    this.setData({
      titlere: e.detail
    });
  },
  onContentInputre: function(e) {
   
    this.setData({
    
      contentre: e.detail
    });
  },
  onAddButtonTapre: function() {
    
    const title2 = this.data.titledel; 
    const content2 = this.data.contentdel; 
    const index = {
      title: title2,
      content: content2
    };
    const newList = this.data.list.filter(item => {
      return item.title !== index.title || item.content !== index.content;
    });

    console.log('Title:', this.data.litlere);
    console.log('Content:', this.data.contentare);
    const title = this.data.titlere; 
    const content = this.data.contentre; 
  
    // 创建新的对象，包装标题和内容
    const newItem = {
      title: title,
      content: content
    };
    // 使用 push() 将新项目添加到数组的末尾
    
    newList.push(newItem);
    // 更新数据
    console.log(newItem)
    this.setData({
      list: newList,
      showre: false
    });
    
  },


  onTitleInput: function(e) {
    this.setData({
      titleadd: e.detail
    });
  },
  onContentInput: function(e) {
   
    this.setData({
    
      contentadd: e.detail
    });
  },
  onAddButtonTap: function() {
   
    
    console.log('Title:', this.data.titleadd);
    console.log('Content:', this.data.contentadd);
    const title = this.data.titleadd; 
    const content = this.data.contentadd; 
  
    // 创建新的对象，包装标题和内容
    const newItem = {
      title: title,
      content: content
    };
    // 使用 push() 将新项目添加到数组的末尾
    let newList = this.data.list;
    newList.push(newItem);
    // 更新数据
    console.log(newItem)
    this.setData({
      list: newList,
      showadd: false
    });
  },
  onComplete(e){
    console.log(e)
    const index = e.detail.name;

    console.log(index)
    const { position, instance } = e.detail;
    switch (position) {
      case 'left':    
          const newList = this.data.list.filter(item => {
          return item.title !== index.title || item.content !== index.content;
        });
        // 更新数据
        this.setData({
          list: newList
        });
        wx.showToast({
          title: '完成事项',
          icon:'success',
          duration:1000,
        })
      instance.close();
      break;
    case 'right':   
    console.log(e)
    console.log(index.title)
    console.log(index.content)
   
    this.setData({ 
      showre: true,
      litlere: index.title,
      contentre: index.content,
      litledel: index.title,
      contentdel: index.content,
      instance: instance
     });
    
     
    }

  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
  },
//   re(e){
// console.log(e)
// this.setData({ showre: true });
//   },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
  },
  onshowadd(){
    this.setData({ showadd: true });
  },
  onCloseadd() {
    this.setData({ showadd: false });
  },
  onClosere() {
    this.setData({ showre: false });
  },
  onChange(e) {
    this.setData({
      result: e.currentTarget.dataset.index,
    });
  },
  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },
  onChange2(event) {
    // event.detail 的值为当前选中项的索引
    this.setData({ active: event.detail });
  },
  toggle(e) {
    console.log(e)
    console.log(e.currentTarget.dataset.index)
    var obj=e.currentTarget.dataset.index
    console.log(obj.content)
    console.log(obj.title)
    
    this.setData({ showcontent: true,
      title: obj.title,
      content:obj.content
    });
    
  },
  closecontent(){
    this.setData({ showcontent: false });
  },
  noop() {},
  updateDate: function() {
    var date = new Date();
    var month = date.getMonth() + 1; // 月份是从0开始的
    var day = date.getDate();
    var dayOfWeek = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"][date.getDay()];
    this.setData({
      currentDate: month + "月" + day + "日 " + dayOfWeek
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.updateDate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})